<?php
namespace app\common\service;

use think\Model;

class ClassCommitteeService extends BaseService
{


    /**
     * 后台: 根据班级ID获取班委列表
     * @param $sid
     * @param $class_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getClassCommitteeListByClassId($sid, $class_id){
        $class_committee_level_list = db('class_committee_level')
                                        ->field('id as class_committee_level_id,name as title_name')
                                        ->where(['sid' => $sid, 'status' => 1])
                                        ->order('sorting ASC')
                                        ->select();

        if($class_committee_level_list){
            $field = 'cs.uid,cs.studentsid,s.name as user_name,s.phone as user_mobile,u.user_headimgurl';
            foreach($class_committee_level_list as $k => $v){
                $where = [
                    'cs.sid' => $sid,
                    'cs.classid' => $class_id,
                    'cs.class_committee_level_id' => $v['class_committee_level_id']
                ];

                $students = db('class_students cs')
                            ->field($field)
                            ->join('students s', 's.id = cs.studentsid')
                            ->join('user u', 'u.user_id = cs.uid', 'LEFT')
                            ->where($where)
                            ->find();
                if($students){
                    $class_committee_level_list[$k] = array_merge($v,$students);
                }
            }
        }
        return $class_committee_level_list;
    }

    /**
     * 后台: 设为班委
     * @param $sid
     * @param $class_id
     * @param $studentsid
     * @param $class_committee_level_id
     * @return array
     */
    public function becomeClassCommittee($sid, $class_id, $studentsid, $class_committee_level_id){
        if($studentsid){
            $condition = [
                'cs.sid' => $sid,
                'cs.studentsid' => $studentsid,
                's.status' => 1,
                's.is_counselor' => 0
            ];
            $class_student = db('class_students cs')
                ->join('students s', 'cs.studentsid = s.id')
                ->where($condition)
                ->find();
            if(!$class_student){
                return ['status' => 0, 'msg' => '此用户信息有误'];
            }
        }

        //取消其他人的职位
        $where = ['sid' => $sid, 'classid' => $class_id, 'class_committee_level_id' => $class_committee_level_id];
        db('class_students')->where($where)->setField('class_committee_level_id', 0);

        //将自己设为班委
        $r = db('class_students')
            ->where(['sid' => $sid, 'classid' => $class_id, 'studentsid' => $studentsid])
            ->setField('class_committee_level_id', $class_committee_level_id);

        return $r === false ? ['status' => 0, 'msg' => '设置失败'] : ['status' => 1, 'msg' => '设置成功'];
    }


    /**
     *
     * 后台: 取消班委头衔
     * @param $sid
     * @param $class_id
     * @param $studentsid
     * @param $class_committee_level_id
     * @return array
     */
    public function cancelClassCommittee($sid, $class_id, $studentsid, $class_committee_level_id){
        $where = [
            'sid' => $sid,
            'studentsid' => $studentsid,
            'class_id' => $class_id,
            'class_committee_level_id' => $class_committee_level_id
        ];
        $r = db('class_students')->where($where)->setField('class_committee_level_id', 0);
        return $r ? ['status' => 1, 'msg' => '取消成功'] : ['status' => 0, 'msg' => '取消失败'];
    }
}







